package com.example.gbasedemo.config;

import cn.hutool.core.date.DateUtil;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Random;

public class DateTimeUtils {
    public static String getTodayStartTime(long minusDays) {
        LocalDateTime today_start = LocalDateTime.of(LocalDate.now().minusDays(minusDays), LocalTime.MIN);
        String startTime = DateUtil.format(today_start, "yyyy-MM-dd HH:mm:ss");
        return startTime;
    }

    public static String getTodayStartDate(long minusDays) {
        LocalDateTime today_start = LocalDateTime.of(LocalDate.now().minusDays(minusDays), LocalTime.MIN);
        String startTime = DateUtil.format(today_start, "yyyy-MM-dd");
        return startTime;
    }


    public static String getTodayEndTime(long plusDay) {
        LocalDateTime today_end = LocalDateTime.of(LocalDate.now().plusDays(plusDay), LocalTime.MAX);
        String endTime = DateUtil.format(today_end, "yyyy-MM-dd HH:mm:ss");
        return endTime;
    }

    public static LocalDateTime generateRandomTime(LocalDateTime startTime, LocalDateTime endTime) {
        // 确保startTime在endTime之前
        if (startTime.isAfter(endTime)) {
            throw new IllegalArgumentException("开始时间不能晚于结束时间");
        }

        // 计算开始时间和结束时间之间的分钟数
        long minutesBetween = ChronoUnit.MINUTES.between(startTime, endTime);

        // 使用随机数生成一个介于0和minutesBetween之间的分钟数
        Random random = new Random();
        long randomMinutes = random.nextLong() % minutesBetween;
        if (randomMinutes < 0) {
            randomMinutes += minutesBetween;
        }

        // 将分钟数添加到开始时间，得到随机时间
        LocalDateTime randomTime = startTime.plusMinutes(randomMinutes);

//        // 确保随机时间在上午10点和下午2点之间
//        if (randomTime.toLocalTime().isBefore(LocalTime.of(10, 0)) ||
//                randomTime.toLocalTime().isAfter(LocalTime.of(14, 0))) {
//            // 如果不在指定时间范围内，递归调用生成新的随机时间
//            return generateRandomTime(startTime, endTime);
//        }
        return randomTime;
    }

//    public static void main(String[] args) {
//        String startTime = getTodayStartTime(1);
//        System.out.println(startTime);
//        String endTime = getTodayEndTime(1);
//        System.out.println(endTime);
//    }

    public static String getNowTime() {
        LocalDateTime today_start = LocalDateTime.now();
        String nowTime = DateUtil.format(today_start, "yyyy-MM-dd HH:mm:ss");
        return nowTime;
    }

    public static String getPlusHoursTime(long plusHours) {
        LocalDateTime today_end = LocalDateTime.now().plusHours(plusHours);
        String nowStartTime = DateUtil.format(today_end, "yyyy-MM-dd HH:mm:ss");
        return nowStartTime;
    }

    public static String getMinusHoursTime(long minusHours) {
        LocalDateTime today_end = LocalDateTime.now().minusHours(minusHours);
        String nowStartTime = DateUtil.format(today_end, "yyyy-MM-dd HH:mm:ss");
        return nowStartTime;
    }

    public static String getPlusMinutesTime(long plusMinutes) {
        LocalDateTime today_end = LocalDateTime.now().plusMinutes(plusMinutes);
        String nowStartTime = DateUtil.format(today_end, "yyyy-MM-dd HH:mm:ss");
        return nowStartTime;
    }
    public static String getMinusMinutesTime(long minusMinutes) {
        LocalDateTime today_end = LocalDateTime.now().minusMinutes(minusMinutes);
        String nowStartTime = DateUtil.format(today_end, "yyyy-MM-dd HH:mm:ss");
        return nowStartTime;
    }

    public static String getThisTime(String time,String format) {
        ZonedDateTime start = ZonedDateTime.of(LocalDateTime.parse(time, DateTimeFormatter.ofPattern(format)), ZoneId.of("Asia/Shanghai"));
        String nowStartTime = start.format(DateTimeFormatter.ofPattern(format));
        return nowStartTime;
    }


    public static String getPlusHoursZeroTime(long plusHours) {
        LocalDateTime today_end = LocalDateTime.now().plusHours(plusHours);
        String nowStartTime = DateUtil.format(today_end, "yyyy-MM-dd HH:00:00");
        return nowStartTime;
    }

    public static String getMinusHoursZeroTime(long minusHours) {
        LocalDateTime today_end = LocalDateTime.now().minusHours(minusHours);
        String nowStartTime = DateUtil.format(today_end, "yyyy-MM-dd HH:00:00");
        return nowStartTime;
    }


    public static String  getNowTimestamp(String time) {
        return convertDateStringToTimestamp(time,"yyyy-MM-dd HH:mm:ss") + "";
    }

    public static long convertDateStringToTimestamp(String dateString, String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        LocalDateTime dateTime = LocalDateTime.parse(dateString, formatter);
        return dateTime.toInstant(ZoneOffset.UTC).toEpochMilli();
    }

    public static String onvertTimeToDateString(String time,String format) {
        ZonedDateTime start = ZonedDateTime.of(LocalDateTime.parse(time, DateTimeFormatter.ofPattern(format)), ZoneId.of("Asia/Shanghai"));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        String nowStartTime = start.format(formatter);
        return nowStartTime;
    }


    // 上一周 开始时间
/*    public static String getLastWeekMinTime(){
        LocalDateTime startOfLastWeek = LocalDateTime.now().with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).with(LocalTime.MIN);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String nowStartTime = startOfLastWeek.format(formatter);
        return  nowStartTime;
    }


    // 上一周 结束时间
    public static String getLastWeekMaxTime(){
        LocalDateTime weekTime = LocalDateTime.now().with(TemporalAdjusters.previous(DayOfWeek.SUNDAY)).with(LocalTime.MAX);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String nowStartTime = weekTime.format(formatter);
        return  nowStartTime;
    }


    // 上一周 开始时间
    public static String getLastWeekMinDate(){
        LocalDateTime startOfLastWeek = LocalDateTime.now().with(TemporalAdjusters.previous(DayOfWeek.MONDAY)).with(LocalTime.MIN);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String nowStartTime = startOfLastWeek.format(formatter);
        return  nowStartTime;
    }


    // 上一周 结束时间
    public static String getLastWeekMaxDate(){
        LocalDateTime weekTime = LocalDateTime.now().with(TemporalAdjusters.previous(DayOfWeek.SUNDAY)).with(LocalTime.MAX);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String nowStartTime = weekTime.format(formatter);
        return  nowStartTime;
    }*/



    public static String getLastMonthMinTime(){
        // 获取当前日期
        LocalDate now = LocalDate.now();
        // 获取上个月的第一天
        LocalDate firstDayOfLastMonth = now.minusMonths(1).withDayOfMonth(1);
        // 获取上个月开始的零时（开始时刻）
        LocalDateTime startOfLastMonth = LocalDateTime.of(firstDayOfLastMonth, LocalDateTime.MIN.toLocalTime());
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return startOfLastMonth.format(formatter2);
    }

    public static String getLastMonthMaxTime(){
        // 获取当前日期
        LocalDate now = LocalDate.now();
        // 获取上个月的最后一天
        LocalDate lastDayOfLastMonth = now.minusMonths(1).withDayOfMonth(now.minusMonths(1).lengthOfMonth());
        // 获取上个月结束的零时（结束时刻）
        LocalDateTime endOfLastMonth = LocalDateTime.of(lastDayOfLastMonth, LocalDateTime.MAX.toLocalTime());
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return endOfLastMonth.format(formatter2);
    }


    public static String getLastMonthMinDate(){
        // 获取当前日期
        LocalDate now = LocalDate.now();
        // 获取上个月的第一天
        LocalDate firstDayOfLastMonth = now.minusMonths(1).withDayOfMonth(1);
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return firstDayOfLastMonth.format(formatter2);
    }

    public static String getLastMonthMaxDate(){
        // 获取当前日期
        LocalDate now = LocalDate.now();
        // 获取上个月的最后一天
        LocalDate lastDayOfLastMonth = now.minusMonths(1).withDayOfMonth(now.minusMonths(1).lengthOfMonth());
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return lastDayOfLastMonth.format(formatter2);
    }

    public  static String convertTimestampToDate(String dateString) {
        LocalDateTime localDateTime = LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return localDateTime.toLocalDate().toString();
    }



    public static void main(String[] args) {
        /*LocalDateTime startTime = LocalDateTime.of(2023, 9, 17, 8, 0); // 开始时间
        LocalDateTime endTime = LocalDateTime.of(2023, 9, 17, 16, 0); // 结束时间

        for (int i = 0; i < 100; i++) {
            LocalDateTime randomTime = generateRandomTime(startTime, endTime);
            System.out.println("随机时间: " + randomTime);
        }

        System.out.println("getTodayStartTime:"+ getTodayStartTime(0));
        System.out.println("getTodayEndTime:"+ getTodayEndTime(0));*/
        //	每周一凌晨两点重新同步上周数据重新入库，
        //	每月30重新同步上月数据重新入库
        // 上一周
        //System.out.println("上一周开始时间：" + getLastWeekMinTime());
        //System.out.println("上一周结束时间：" + getLastWeekMaxTime());

        System.out.println("上一月开始时间：" + getLastMonthMinTime());
        System.out.println("上一月结束时间：" + getLastMonthMaxTime());


        //System.out.println("上一周开始时间：" + convertDateStringToTimestamp(getLastWeekMinTime(),"yyyy-MM-dd HH:mm:ss"));
        //System.out.println("上一周结束时间：" + convertDateStringToTimestamp(getLastWeekMaxTime(),"yyyy-MM-dd HH:mm:ss"));

        //System.out.println("上一月开始时间：" + convertDateStringToTimestamp(getLastMonthMinTime(),"yyyy-MM-dd HH:mm:ss"));
        //System.out.println("上一月结束时间：" + convertDateStringToTimestamp(getLastMonthMaxTime(),"yyyy-MM-dd HH:mm:ss"));



        //System.out.println("上一月开始时间：" + getLastMonthMinDate());
        //System.out.println("上一月结束时间：" + getLastMonthMaxDate());
        //System.out.println("上一周开始时间：" + getLastWeekMinDate());
        //System.out.println("上一周结束时间：" + getLastWeekMaxDate());


/*        String formatStart = LocalDateTime.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String formatEnd = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println(formatStart+"====="+formatEnd);

        String formatStartTime = LocalDateTime.now().minusDays(1).with(LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String formatEndTime = LocalDateTime.now().with(LocalTime.MAX).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println("formatStartTime===:"+formatStartTime+"=====formatEndTime=====:"+formatEndTime);


        System.out.println("开始时间：" + convertDateStringToTimestamp(formatStartTime,"yyyy-MM-dd HH:mm:ss"));
        System.out.println("结束时间：" + convertDateStringToTimestamp(formatEndTime,"yyyy-MM-dd HH:mm:ss"));*/



        //计算开始时间和结束时间
        String startTime = "2024-03-14";
        String endTime = "2024-03-14";
//        String startTimes = getNowTimestamp(startTime);
//        System.out.println("startTime="+startTime+",endTime="+endTime);
//        System.out.println("startTimes:"+startTimes);

        LocalDate startOfDay = LocalDate.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDateTime startThisTime = startOfDay.atStartOfDay().with(LocalTime.MIN);
        LocalDate endOfDay = LocalDate.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDateTime endThisTime = endOfDay.atStartOfDay().with(LocalTime.MAX);

        String startTimestamp = startThisTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String endTimestamp =  endThisTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println(startTimestamp+ "==="+ endTimestamp);
        System.out.println(DateTimeUtils.getNowTimestamp(startTimestamp)+ "==="+ DateTimeUtils.getNowTimestamp(endTimestamp));

        System.out.println(convertTimestampToDate("2024-03-26 12:00:00"));
    }




}
